(function($){
	var $coc=$('#dtocooperorgCode'),
		$initiator=$('#dtoinitiator'),
		$receiver=$('#dtoreceiver'),
		$payer=$('#dtopayer'),
		$gathering=$('#dtogathering'),
		data,
		flag=true;
	 var append=function(el,options){
		 el.empty();
		 el.append(options.join(""));
		 if(flag){
			 el.children("option[value="+el.attr("dtovalue")+"]").prop("selected",true);
		 }
	 };
	 window.cascadeCooperOrgAgreement=function(masterId,slaveId){
		var cooperOrgSetting={
			url:'/cimsCascade/queryListCimsCascadeCooperorg',
			displayField:'cooperorgName',
			valueField:'cooperorgCode',
			id:masterId,
			controlMap:{"cooperorgCode":"cooperorgCode"},
			onChange:function(combo,data){
				var array=[Cmm.JY,Cmm.JJ,data],
					options=["<option value=''>--请选择--</option>"];
				$.each(array,function(index,item){
					options.push("<option value="+item.cooperorgCode+">"+item.cooperorgName+"</option>");
				});
				data=array;
				append($initiator,options);
				append($receiver,options);
				append($payer,options);
				append($gathering,options);
				flag&&(flag=!flag);
			}
		};
		var agreementSetting={
				url:'/tCMmAgreement/queryListTCMmAgreement',
				displayField:'agreementName',
				valueField:'agreementCode',
				id:slaveId
		};
		Cmm.comboCascade(cooperOrgSetting,agreementSetting);
	};
	var changeFun=function($Obj){
		return function(){
			var nSelected=$(this).children("option:not(:selected)"),
			htmlArray=[];
			$.each(nSelected,function(index,item){
				htmlArray.push(item.outerHTML);
			});
			if(nSelected.length==2){
				$Obj.empty();
				$Obj.append(htmlArray.join(""));
			}
		};
	};
	$initiator.on('change',changeFun($receiver));
	$receiver.on('change',changeFun($initiator));
	
	$payer.on('change',changeFun($gathering));
	$gathering.on('change',changeFun($payer));
	
})(jQuery);